package com.rayboot.footpoint.utils.voice;

import java.io.File;
import java.io.IOException;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

public class FootprintVoiceManager {
	private static final boolean DEBUG = true;
	static final String MIME_TYPE = "audio/amr";
	private static final String TAG = "FootprintImageManager";
	private static FootprintVoiceManager sInstance = null;

	private static boolean checkFsWritable() {
		String str = Environment.getExternalStorageDirectory().toString()
				+ "/.footprints/voices";
		File file = new File(str);

		if (!file.isDirectory() && !file.mkdirs()) {
			return false;
		}

		File probeFile = new File(str, ".probe");
		try {
			if (probeFile.exists()) {
				probeFile.delete();
			}

			if (!probeFile.createNewFile()) {
				return false;
			}

			probeFile.delete();
			return true;
		} catch (IOException localIOException) {
			return false;
		}
	}

	public static String getDirectory() {
		return Environment.getExternalStorageDirectory()
				+ "/.footprints/voices";
	}

	public static FootprintVoiceManager getInstance() {
		if (sInstance == null)
			sInstance = new FootprintVoiceManager();
		return sInstance;
	}

	public static boolean hasStorage() {
		return hasStorage(true);
	}

	public static boolean hasStorage(boolean paramBoolean) {
		String str = Environment.getExternalStorageState();
		Log.d(TAG, "state is " + str);

		if ("mounted".equals(str)) {
			if (paramBoolean) {
				if (!checkFsWritable()) {
					return false;
				}
			}
		}

		if ((!paramBoolean) && ("mounted_ro".equals(str))) {
			return true;
		}
		return false;
	}

	public static Cursor query(Context context, Uri uri, String[] arrayString1,
			String str1, String[] arrayString2, String str2) {
		try {
			ContentResolver contentResolver = context.getContentResolver();
			if (contentResolver == null) {
				return null;
			} else {
				Cursor localCursor = contentResolver.query(uri, arrayString1,
						str1, arrayString2, str2);
				return localCursor;
			}
		} catch (UnsupportedOperationException localUnsupportedOperationException) {

		}
		return null;
	}
}
